package com.tomato.dao;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.tomato.bean.Ebook;

public class EbookDao {
	private static EbookDao _instance = null;

	public static EbookDao getInstance() {
		if (_instance == null) {
			_instance = new EbookDao();
		}
		return _instance;
	}

	//save photo
	public String insertEbook(Ebook ebook) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(ebook);
		} finally {
			pm.close();
		}
		return ebook.getId().toString();
	}
	
	//get photo by id
	@SuppressWarnings("unchecked")
	public Ebook getById(Long id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Ebook.class);
		query.setFilter("id == idParam");
		query.declareParameters("Long idParam");
		List<Ebook> ebook = null;
		try {
			ebook = (List<Ebook>) query.execute(id);
			if (ebook.isEmpty()){
				return null;
			}else{
				return ebook.get(0);
			}
			
		} finally {
			query.closeAll();
		}
	}
	//get photo by id
	public Ebook getByPid(Long id){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Ebook ebook = pm.getObjectById(Ebook.class,id);
		return ebook;
	}

	//get photo list
	@SuppressWarnings("unchecked")
	public List<Object[]> getAllByPage(int indexStart, int indexEnd) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Ebook.class);
		query.setRange(indexStart, indexEnd);
		query.setOrdering("date desc");
		query.setResult("id, title, date, description");
		List<Object[]> ebook = new ArrayList();
		try {
			ebook = (List<Object[]>) query.execute();
			return ebook;
		} finally {
			query.closeAll();
		}
	}

	// get counts
	public int getCount() {
		String sql = "SELECT count(id) FROM com.tomato.bean.Ebook";
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(sql);
		int i;
		try {
			i = (Integer) query.execute();
			return i;
		} finally {
			query.closeAll();
		}
	}
	
	// delete photo by id
	@SuppressWarnings("unchecked")
	public boolean deleteById(Long id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Ebook.class);
		query.setFilter("id == idParam");
		query.declareParameters("Long idParam");
		List<Ebook> ebook = (List<Ebook>) pm.newQuery(query).execute(id);
		try {
			for (Ebook ebooks : ebook) {
				pm.deletePersistent(ebooks);
			}
		} finally {
			pm.close();
		}
		return true;
	}

	/**
	 * @param photo
	 */
	//update photo property
	public void update(Ebook ebook) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Ebook ebooks = pm.getObjectById(Ebook.class, ebook.getId());
			ebooks.setImage(ebook.getImage());
		} finally {
			pm.close();
		}		
	}
	//count update
	public void countUpdate(Ebook ebook) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Ebook ebooks = pm.getObjectById(Ebook.class, ebook.getId());
			ebooks.setCount(ebook.getCount());		
		} finally {
			pm.close();
		}
		
	}
}
